package com.axx.android.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.BitmapImageViewTarget;

/**
 * Created by Administrator on 2017/6/4 0004.
 */

public class ImageLoader {
    public static void loadImage(String url, ImageView imageView, int placeholder, Context context) {
        Glide.with(context).load(url).centerCrop().placeholder(placeholder).dontAnimate().into(imageView);
    }

    public static void loadImage(String url, ImageView imageView, Context context) {
        loadImage(url, imageView,0, context);
    }

    public static void loadRoundImage(String url, final ImageView imageView, final Context context, int placeholder) {
        loadRadiusImage(url, imageView, context, placeholder, 0);
    }

    public static void loadRoundImage(String url, final ImageView imageView, final Context context) {
        loadRadiusImage(url, imageView, context, 0, 0);
    }

    public static void loadRadiusImage(String url, final ImageView imageView, final Context context, final int radius) {
        loadRadiusImage(url, imageView, context, 0, radius);
    }

    public static void loadRadiusImage(String url, final ImageView imageView, final Context context, int placeholder, final int radius) {
        Glide.with(context).load(url).asBitmap().diskCacheStrategy(DiskCacheStrategy.ALL).centerCrop().placeholder(placeholder).into(new BitmapImageViewTarget(imageView) {
            @Override
            protected void setResource(Bitmap resource) {
                RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                if (radius == 0) {
                    circularBitmapDrawable.setCircular(true);
                    circularBitmapDrawable.setAntiAlias(true);
                } else {
                    circularBitmapDrawable.setCornerRadius(radius);
                }
                imageView.setImageDrawable(circularBitmapDrawable);
            }
        });
    }
}
